package ie.dkit.semanticPhotoWeb.client.panels.profile;

import ie.dkit.semanticPhotoWeb.client.dtos.InterestDTO;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;

import com.google.gwt.user.client.ui.HTML;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.MessageBox.ConfirmCallback;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.HorizontalLayout;

public class InterestPanel extends Panel {

	private final EditUserProfileWindow m_EditUserProfileWindow;
	
	protected InterestPanel(final EditUserProfileWindow editUserProfileWindow) {
		super();
		this.m_EditUserProfileWindow = editUserProfileWindow;
		setTitle("Interests");
		setCollapsible(true);
		addButton(new Button("Add Interest", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				new Window() {
					TextField title;

					TextField about;

					InterestDTO interestDTO;

					Window thisAno;

					{
						thisAno = this;
						setTitle("Add Interest");
						setWidth(400);
						setHeight(200);
						setModal(true);
						setResizable(true);
						FormPanel formPanel = new FormPanel();
						formPanel.add(title = new TextField("Title"));
						title.setAllowBlank(false);
						formPanel.add(about = new TextField("About"));
						add(formPanel);
						add(new Button("Save", new ButtonListenerAdapter() {
							public void onClick(Button button, EventObject e) {
								interestDTO = new InterestDTO(title
										.getText(), about.getText());
								m_EditUserProfileWindow.m_ClientServerConnectorAsync.addInterest(
										interestDTO,
										new AsyncCallbackAdapter() {
											public void onSuccess(
													Object result) {
												InterestPanel.this
														.addInterest(interestDTO);
												InterestPanel.this
														.doLayout();
												thisAno.close();
											}
										});
							}
						}));
					}
				}.show();
			}
		}));
	}

	protected void addInterests(InterestDTO[] interests) {
		for (int i = 0; i < interests.length; i++) {
			addInterest(interests[i]);
		}
		doLayout();
	}

	private void addInterest(final InterestDTO interestDTO) {
		final Panel panel = new Panel();
		panel.setLayout(new HorizontalLayout(1));
		panel.add(new HTML(interestDTO.title + " : " + interestDTO.about));
		panel.add(new Button("X", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				MessageBox
						.confirm(
								"Delete",
								"Are you sure you want to delete the Relation to this Person?",
								new ConfirmCallback() {
									public void execute(String btnID) {
										if (btnID.equals("yes")) {
											m_EditUserProfileWindow.m_ClientServerConnectorAsync.deleteInterest(interestDTO, new AsyncCallbackAdapter() {
												public void onSuccess(Object result) {
													remove(panel);
													doLayout();
												}													
											});
										}
									}
								});
			}
		}));
		add(panel);
	}
}
